package com.broadsoft.platform.wfengine.runtime.nodehandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broadsoft.platform.wfengine.NodeType;
import com.broadsoft.platform.wfengine.parser.ParsedWorkflowDefinition;
import com.broadsoft.platform.wfengine.runtime.WfContext;
import com.broadsoft.platform.wfengine.runtime.impl.NodeExecutionServiceImpl;
import com.broadsoft.platform.wfengine.runtime.spi.DecisionHandler;


/**
 * 
* FIXME: <A few lines of description of what class is about and what it does.> 
*
* <HR/><B>Modification Block</B>
* <P>
* Version: FIXME: 1.0.1 <BR>
* Author: FIXME: luzhen
* Date: 2011-9-16
* TAG: <A short name to find out all changed made by this modification.>
* Comments: Created.
* Bug fix NO.: <If required>
* </P>
* <P>
* ... Other Modifications following above structure.
* </P>
*
 */
public class DecisionNodeExecutionHandler implements INodeExecutionHandler {

    private final static Logger logger = LoggerFactory.getLogger(NodeExecutionServiceImpl.class);

    public DecisionNodeExecutionHandler(ParsedWorkflowDefinition parsedWorkflowInfo,DecisionHandler decisionHandler){
        this.parsedWorkflowInfo=parsedWorkflowInfo;
        this.decisionHandler=decisionHandler;
        
    }
    
    @Override
    public NodeType executeNode(NodeType currentNode, WfContext flowContext) {
        if (logger.isTraceEnabled()) {
            logger.trace("The method[executeConditionNode] begin");
        }
        flowContext.setPreviousNode(currentNode);
        // invoke the condition expression or action bean
        String nextNodeName = decisionHandler.handle(currentNode, flowContext);
        NodeType nextNodeType = parsedWorkflowInfo
                .getNodeTypeByName(flowContext.getWorkflowType().getName(), nextNodeName);
        if (logger.isTraceEnabled()) {
            logger.trace("The local variable [nextNode] is {}.", nextNodeType.getName());
            logger.trace("The method[executeConditionNode] end");
        }
        return nextNodeType;
    }

    private ParsedWorkflowDefinition parsedWorkflowInfo;

    public ParsedWorkflowDefinition getParsedWorkflowInfo() {
        return parsedWorkflowInfo;
    }

    public void setParsedWorkflowInfo(ParsedWorkflowDefinition parsedWorkflowInfo) {
        this.parsedWorkflowInfo = parsedWorkflowInfo;
    }

    private DecisionHandler decisionHandler;

    public DecisionHandler getDecisionHandler() {
        return decisionHandler;
    }

    public void setDecisionHandler(DecisionHandler decisionHandler) {
        this.decisionHandler = decisionHandler;
    }

}
